package org.tmsps.ne4spring.utils.express;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.tmsps.ne4spring.utils.http.HttpUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * 查询物流接口
 * 
 * @author Administrator
 *
 */
/**
快递公司编号(companyCode)	快递公司名称
1	顺丰
2	申通
3	圆通
4	韵达
5	天天
6	EMS
7	中通
8	汇通
9	全峰
10	德邦
11	国通
12	如风达
13	京东快递
14	宅急送
15	EMS国际
16	Fedex国际
17	邮政国内（挂号信）
18	UPS国际快递
19	中铁快运
20	佳吉快运
21	速尔快递
22	信丰物流
23	优速快递
24	中邮物流
25	天地华宇
26	安信达快递
27	快捷速递
28	AAE全球专递
29	DHL
30	DPEX国际快递
31	D速物流
32	FEDEX国内快递
33	OCS
34	TNT
35	东方快递
36	传喜物流
37	城市100
38	城市之星物流
39	安捷快递
40	百福东方
41	程光快递
42	递四方快递
43	长通物流
44	飞豹快递
45	马来西亚（大包EMS）
46	安能快递
 */
public class ExpressUtils {
	private static final String appcode = "4b92b785dced418c8bdf5ba36fc474ff";

	
	
	public static String getLogistics(String code,long num) {
		String host = "http://expressq.market.alicloudapi.com";
		String path = "/clouds/query/expressQuery";
		String method = "POST";
		Map<String, String> headers = new HashMap<String, String>();
		// 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		// 根据API的要求，定义相对应的Content-Type
		headers.put("Content-Type", "application/json; charset=UTF-8");
		Map<String, String> querys = new HashMap<String, String>();
		JSONObject jobj = new JSONObject();
		jobj.put("companyCode", code);
		jobj.put("expressNum",num);
		//String bodys = "{\"companyCode\":2,\"expressNum\":\"3379011174391\"}";
		String bodys = jobj.toJSONString();
		String body = "";
		try { 
			/**
			 * 重要提示如下: HttpUtils请从
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
			 * 下载
			 *
			 * 相应的依赖请参照
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
			 */
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			//System.out.println(response.toString());
			// 获取response的body
			body = EntityUtils.toString(response.getEntity());
			//System.err.println(body);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return body;
	}
	 public static final String QUERYURL = "http://www.kuaidi100.com/query?";
	public static String setUrl(String logisticsCode, String logisticsNo) {
        String temp = String.valueOf(Math.random());
        StringBuilder sb = new StringBuilder(QUERYURL);
        sb.append("tpye=").append(logisticsCode).append("&");
        sb.append("postid=").append(logisticsNo).append("&");
        sb.append("temp=").append(temp);
        return sb.toString();
    }
	 public static String queryData(String logisticsCode, String logisticsNo) {
	        String line = "";
	        String temp = String.valueOf(Math.random());
	        String url = "http://www.kuaidi100.com/query?type=" + logisticsCode + "&postid=" + logisticsNo + "&temp=" + temp + "";
	        try {
	            URL realURL = new URL(url);
	            HttpURLConnection conn = (HttpURLConnection) realURL.openConnection();
	            conn.setRequestProperty("accept", "*/*");
	            conn.setRequestProperty("connection", "Keep-Alive");
	            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
	            conn.connect();
	            int responseCode = conn.getResponseCode();
	            if (responseCode == HttpURLConnection.HTTP_OK) {
	                InputStream inputStream = conn.getInputStream();
	                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
	                while ((line = reader.readLine()) != null) {
	                	
	                    return line;
	                }
	                reader.close();
	                conn.disconnect();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        JSONObject obj = JSONObject.parseObject(line);
	        return obj.toJSONString();

	    }
	public static void main(String[] args) {
		
		String str =queryData("shunfeng","764368395209");
		JSONObject obj = JSONObject.parseObject(str);
		System.out.println(obj);
		
	/*	String host = "http://expressq.market.alicloudapi.com";
		String path = "/clouds/query/expressQuery";
		String method = "POST";
		Map<String, String> headers = new HashMap<String, String>();
		// 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		headers.put("Access-Control-Allow-Origin", "*");
		// 根据API的要求，定义相对应的Content-Type
		headers.put("Content-Type", "application/json; charset=UTF-8");
		Map<String, String> querys = new HashMap<String, String>();
		JSONObject jobj = new JSONObject();
		jobj.put("companyCode", 2);
		jobj.put("expressNum","3379011174391");
		//String bodys = "{\"companyCode\":2,\"expressNum\":\"3379011174391\"}";
		String bodys = jobj.toJSONString();
		try {
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			System.out.println(response.toString());
			// 获取response的body
			String body = EntityUtils.toString(response.getEntity());
			System.err.println(body);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
}
